﻿using System;
using System.Collections.Generic;
using System.Linq;
using JinRi.Services.Domains;

namespace JinRi.Services
{
    public class DataService
    {
        private readonly JinRiContext _db = new JinRiContext();

        public bool CheckUser(string username, string password)
        {
            username = username.ToLower();
            password = password.ToLower();
            return _db.Users.Any(p => p.Username == username && p.Password == password && !p.IsDeleted);
        }

        public T Delete<T>(Guid id) where T : Domain
        {
            var find = _db.Set<T>().FirstOrDefault(p => p.Id == id && !p.IsDeleted);
            if (find != null)
            {
                find.IsDeleted = true;
                find.UpdateTime = DateTime.Now;
                _db.SaveChanges();
            }
            return find;
        }

        private T Update<T>(T entity) where T : Domain
        {
            entity.UpdateTime = DateTime.Now;
            _db.SaveChanges();
            return entity;
        }

        private T Create<T>(T entity) where T : Domain
        {
            _db.Set<T>().Add(entity);
            _db.SaveChanges();
            return entity;
        }

        private T CreateOrUpdate<T>(T entity) where T : Domain
        {
            var item = Find<T>(entity.Id);
            if (item == null)
            {
                return Create(entity);
            }
            else
            {
                return Update(entity);
            }
        }

        public T Find<T>(Guid id) where T : Domain
        {
            var find = _db.Set<T>().FirstOrDefault(p => p.Id == id && !p.IsDeleted);
            return find;
        }

        public List<T> FindAll<T>() where T : Domain
        {
            var list = _db.Set<T>()
                .Where(p => !p.IsDeleted)
                .OrderByDescending(p => p.CreateTime)
                .ToList();
            return list;
        }

        public Image AddImage(byte[] bytes)
        {
            var model = new Image
            {
                Bytes = bytes
            };
            Create(model);
            return model;
        }

        public Award AddAward(string title, string content, Guid imageId)
        {
            var model = new Award
            {
                Title = title,
                Content = content,
                ImageId = imageId
            };
            Create(model);
            return model;
        }

        public Award UpdateAward(Guid id, string title, string content, Guid imageId)
        {
            var find = Find<Award>(id);
            if (find != null)
            {
                find.Title = title ?? find.Title;
                find.Content = content ?? find.Content;
                find.ImageId = imageId;
                Update(find);
            }
            return find;
        }

        public About GetAbout()
        {
            var item = FindAll<About>().FirstOrDefault();
            return item;
        }

        public void SetAbout(Guid bannerImageId, string introduce, string name, string title, string content, Guid imageId)
        {
            var item = FindAll<About>().FirstOrDefault();
            if (item == null)
            {
                item = new About();
            }
            item.BannerImageId = bannerImageId;
            item.Introduce = introduce;
            item.Name = name;
            item.Title = title;
            item.Content = content;
            item.ImageId = imageId;
            CreateOrUpdate(item);
        }

        public Product AddProduct(string title, string introduce, string content, Guid bannerImageId, Guid coverImageId)
        {
            var model = new Product
            {
                Title = title,
                Content = content,
                BannerImageId = bannerImageId,
                CoverImageId = coverImageId,
                Introduce = introduce,
            };
            Create(model);
            return model;
        }

        public Product UpdateProduct(Guid id, string title, string introduce, string content, Guid bannerImageId, Guid coverImageId)
        {
            var find = Find<Product>(id);
            if (find != null)
            {
                find.Title = title ?? find.Title;
                find.Content = content ?? find.Content;
                find.BannerImageId = bannerImageId;
                find.CoverImageId = coverImageId;
                find.Introduce = introduce;
                Update(find);
            }
            return find;
        }

        public Video AddVideo(string title, Guid coverImageId, string url)
        {
            var model = new Video
            {
                Title = title,
                CoverImageId = coverImageId,
                Url = url
            };
            Create(model);
            return model;
        }

        public Video UpdateVideo(Guid id, string title, Guid coverImageId, string url)
        {
            var find = Find<Video>(id);
            if (find != null)
            {
                find.Title = title ?? find.Title;
                find.CoverImageId = coverImageId;
                find.Url = url;
                Update(find);
            }
            return find;
        }

        public News UpdateNews(Guid id, string title, int category, string content)
        {
            var find = Find<News>(id);
            if (find != null)
            {
                find.Title = title ?? find.Title;
                find.Content = content;
                find.Category = category;
                Update(find);
            }
            return find;
        }

        public News AddNews(string title, int category, string content)
        {
            var model = new News
            {
                Title = title,
                Category = category,
                Content = content
            };
            Create(model);
            return model;
        }

        public Banner GetBanner(int category)
        {
            var item = FindAll<Banner>().FirstOrDefault(p => p.Category == category);
            return item;
        }

        public Banner SetBanner(int category, Guid imageId,string introduce)
        {
            var find = FindAll<Banner>().FirstOrDefault(p => p.Category == category);
            if (find == null)
            {
                find = new Banner();
            }
            find.ImageId = imageId;
            find.Category = category;
            find.Introduce = introduce;
            CreateOrUpdate(find);
            return find;
           
        }

        public Job UpdateJob(Guid id, string title, string content)
        {
            var find = Find<Job>(id);
            if (find != null)
            {
                find.Title = title ?? find.Title;
                find.Content = content;
                Update(find);
            }
            return find;
        }

        public Job AddJob(string title, string content)
        {
            var model = new Job
            {
                Title = title,
                Content = content
            };
            Create(model);
            return model;
        }

        public Contact GetContact()
        {
            var item = FindAll<Contact>().FirstOrDefault();
            return item;
        }

        public Contact SetContact(string content, Guid imageId)
        {
            var item = FindAll<Contact>().FirstOrDefault();
            if (item == null)
            {
                item = new Contact();
            }
            item.Content = content;
            item.ImageId = imageId;
            CreateOrUpdate(item);
            return item;
        }

        public List<Guid> GetImageIds()
        {
            var list = _db.Images
                .Where(p => !p.IsDeleted)
                .OrderByDescending(p => p.CreateTime)
                .Select(p => p.Id)
                .ToList();
            return list;
        }
    }
}